/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatWindowFrame.java
 *
 * Created on 14/07/2011, 16:50:41
 */
package chatWindow;


import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;


/**
 *
 * @author linux
 */
public class ChatWindowFrame extends javax.swing.JInternalFrame {

    /** Creates new form ChatWindowFrame */
    public ChatWindowFrame() {
        model =  new  DefaultListModel();
        initComponents();
        bSend.requestFocus();
        
        //setFrameIcon(ICON_CHAT);
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Discuss_16.png")));
    }
    
    //Make textField get the focus whenever frame is activated.

    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bSend = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        taAllChat = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        taOwnChat = new javax.swing.JTextArea();
        jLabel1 = new javax.swing.JLabel();
        loginName = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        jlAllMembers = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jbLogout = new javax.swing.JButton();

        setTitle("ChatRoom Window");

        bSend.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Send_16.png"))); // NOI18N
        bSend.setText("Send");
        bSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bSendActionPerformed(evt);
            }
        });

        taAllChat.setColumns(20);
        taAllChat.setEditable(false);
        taAllChat.setLineWrap(true);
        taAllChat.setRows(5);
        jScrollPane1.setViewportView(taAllChat);

        taOwnChat.setColumns(20);
        taOwnChat.setLineWrap(true);
        taOwnChat.setRows(5);
        jScrollPane2.setViewportView(taOwnChat);

        jLabel1.setFont(new java.awt.Font("Ubuntu", 0, 12)); // NOI18N
        jLabel1.setText("You are logged as:");

        loginName.setFont(new java.awt.Font("Ubuntu", 2, 15));

        jlAllMembers.setFont(new java.awt.Font("Ubuntu", 2, 15));
        jlAllMembers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(jlAllMembers);

        jLabel2.setFont(new java.awt.Font("Ubuntu", 1, 12)); // NOI18N
        jLabel2.setText("All Members");

        jbLogout.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/Lock_16.png"))); // NOI18N
        jbLogout.setText("Logout");
        jbLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbLogoutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(bSend))
                            .addComponent(jScrollPane1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(loginName, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbLogout)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(loginName, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jbLogout)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(36, 36, 36)
                                .addComponent(bSend)))))
                .addContainerGap(26, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSendActionPerformed
        if(taOwnChat.getText().compareTo("") == 0){
            JOptionPane.showInternalMessageDialog(this, "You must write a non empty message to send.");
        }else{
            
            ChatWindowFrameController.getInstance().sendMsg(taOwnChat.getText()); 
            
            //taAllChat.setText(taAllChat.getText()+"\n"+getLoginName()+" Say: "+taOwnChat.getText());
            taOwnChat.setText("");
            taOwnChat.requestFocusInWindow();
        }
    }//GEN-LAST:event_bSendActionPerformed


    private void jbLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbLogoutActionPerformed
        // TODO add your handling code here:
        int choice = JOptionPane.showConfirmDialog(this, "You are going to leave the room. \n The application will close. \n Do you wish to continue?");
            if(choice == JOptionPane.YES_OPTION){
                ChatWindowFrameController.getInstance().logout(getLoginName());
                dispose();
                System.exit(0);
            }
    }//GEN-LAST:event_jbLogoutActionPerformed

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JButton jbLogout;
    private javax.swing.JList jlAllMembers;
    private javax.swing.JLabel loginName;
    private javax.swing.JTextArea taAllChat;
    private javax.swing.JTextArea taOwnChat;
    // End of variables declaration//GEN-END:variables
    DefaultListModel model =  new DefaultListModel();
    
    public String getLoginName() {
        return loginName.getText();
    }

    public void setLoginName(String loginName) {
        this.loginName.setText(loginName);
    }

    public String getAllChat() {
        return taAllChat.getText();
    }

    public void setAllChat(String AllChat) {
        this.taAllChat.setText(AllChat);
    }

    public String getOwnChat() {
        return taOwnChat.getText();
    }

    public void setOwnChat(String OwnChat) {
        this.taOwnChat.setText(OwnChat);
    }

    public JList getJlAllMembers() {
        return jlAllMembers;
    }

    public void setJlAllMembers(JList jlAllMembers) {
        this.jlAllMembers = jlAllMembers;
    }
    
    public void addToAllMembers(String member){
         //DefaultListModel model =  new  DefaultListModel();
         model.addElement(member); /*load all members*/
         ChatWindowFrameController.getInstance().getCwFrame().getJlAllMembers().setModel(model);
    }
    
    public void removeFromAllMembers(String member){
        model.removeElement(member);
        ChatWindowFrameController.getInstance().getCwFrame().getJlAllMembers().setModel(model);
    }
    
    public void addAllMembers(String members[]){
        int i = 0;
        while(!members[i].isEmpty()){
            model.addElement(members[i]);
            System.out.println("member: "+i+" Username: "+members[i]);
            i++;
        }
        ChatWindowFrameController.getInstance().getCwFrame().getJlAllMembers().setModel(model);
    }

    public DefaultListModel getModel() {
        return model;
    }

    public void setModel(DefaultListModel model) {
        this.model = model;
    }

}
